<?php

/***
 * Class ApplicationUtilityClass validates and determines the current, valid
 * controller and application to load.
 ***/
class CW_ApplicationClass {

   /**
	* Core registry
	* @access Private
	* @var Object
	*/
	private $Reg;


   /**
	* Currently loaded application
	* @access Public
	* @var String
	*/
	public $current_application;


   /**
	* Currently loaded controller
	* @access Public
	* @var String
	*/
	public $current_controller;


   /**
	* List of all installed applications
	* @access Private
	* @var Array
	*/
	private $installed_applications;


   /**
	* List of all controllers within the current application
	* @access Private
	* @var Array
	*/
	private $installed_controllers;


   // ! Constructor Method

   /**
	* Instantiates class and defines instance variables.
	*
	* @param Object $Reg Core system registry
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access public
	* @return Void
	*/
	public function __construct(&$Reg)
	{
		$this->Reg = &$Reg;

		$this->current_application = $Reg->Config->get('application', 'default_application');
		$this->current_controller  = $Reg->Config->get('application', 'default_controller');
		$this->installed_applications = array();
		$this->installed_controllers = array();
	}


   // ! Mutator Method

   /**
	* Sets the default controller
	*
	* @param String $controller Name of the default controller
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function setDefaultController($controller)
	{
		$this->current_controller = $controller;

		return true;
	}


   // ! Mutator Method

   /**
	* Sets the default application
	*
	* @param String $application Name of the default application
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return True
	*/
	public function setDefaultApplication($application)
	{
		$this->current_application = $application;

		return true;
	}


   // ! Mutator Method

   /**
	* Validates the specified application against a list of currently
	* installed applications. If a match is not made, switch to the
	* system default application.
	*
	* @param String $supposed_application Name of the application to validate
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return String
	*/
	public function setCurrentApplication($supposed_application)
	{
		if(false == $this->getAllApplications())
		{
			throw new CoreException("No applications could be found within the applications directory!");
		}

		if(false == $this->doesApplicationExist($supposed_application))
		{
			$original_application = $supposed_application;
			$supposed_application = $this->Reg->Config->get('application', 'default_application');

			if(false == $this->doesApplicationExist($supposed_application))
			{
				throw new CoreException("The requested application '{$original_application}' and the default application could not be located.");
			}
		}

		$this->current_application = $supposed_application;

		return $this->current_application;
	}


   // ! Mutator Method

   /**
	* Validates the specified controller against a list of currently
	* installed controllers within the specified application. If a
	* match is not made, switch to the system default application.
	*
	* @param String $application Application to validate the controller against
	* @param String $supposed_controller The name of the controller to validate
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return String
	*/
	public function setCurrentController($application, $supposed_controller)
	{
		if(false == $this->doesApplicationExist($application))
		{
			throw new CoreException("Application '{$application}' does not have any controllers installed!");
		}

		if(false == $this->doesControllerExist($application, $supposed_controller))
		{
			$original_controller = $supposed_controller;
			$supposed_controller = $this->current_controller;;

			if(false == $this->doesControllerExist($application, $supposed_controller))
			{
				throw new CoreException("The requested controller '{$original_controller}' and the default controller for application '{$application}' could not be located.");
			}
		}

		$this->current_controller = $supposed_controller;

		return $this->current_controller;
	}


   // ! Accessor Method

   /**
	* Creates a list of all currently installed applications.
	*
	* @param None
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Array
	*/
	public function getAllApplications()
	{
		if(false == $this->installed_applications)
		{
			foreach(new DirectoryIterator(PATH_APPS) as $file)
			{
				if($file->isDir() && false == in_array($file->getFileName(), array('.', '..')))
				{
					$this->installed_applications[] = $file->getFileName();
				}
			}
		}

		return $this->installed_applications;
	}


   // ! Executor Method

   /**
	* Checks if specified $application exists within the system.
	*
	* @param String $application Name of the application to search for
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Boolean
	*/
	public function doesApplicationExist($application)
	{
		if(false == in_array($application, $this->getAllApplications()))
		{
			return false;
		}

		return true;
	}


   // ! Accessor Method

   /**
	* Creates a list of all controllers within the specified application.
	*
	* @param String $application Name of the application to search under
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Mixed
	*/
	public function getAllControllers($application)
	{
		if(false == $this->doesApplicationExist($application))
		{
			return false;
		}

		if(false == $this->installed_controllers)
		{
			foreach(new DirectoryIterator(PATH_APPS . $application . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR) as $file)
			{
				if(false == $file->isDir())
				{
					$controller_bits = explode('.', $file->getFileName());

					if(end($controller_bits) == str_replace('.', '', PHP_EXT) && $controller_bits[1] == 'controller')
					{
						$this->installed_controllers[] = $controller_bits[0];
					}
				}
			}
		}

		return $this->installed_controllers;
	}


   // ! Executor Method

   /**
	* Checks if specified $controller exists within the $application.
	*
	* @param String $application Name of the application to search under
	* @param String $controller Name of the controller to search for
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Public
	* @return Boolean
	*/
	public function doesControllerExist($application, $controller)
	{
		if(false == $this->doesApplicationExist($application))
		{
			return false;
		}

		if(false == in_array($controller, $this->getAllControllers($application)))
		{
			return false;
		}

		return true;
	}
}

?>